package zz.app.ustbgsmanager.adapters;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import zz.app.ustbgsmanager.R;
import zz.app.ustbgsmanager.beans.querycourse.CourseInfo;

/**
 * Created by zz on 2018/6/8 16:54.
 */
public class CourseRecyclerViewAdapter extends RecyclerView.Adapter<CourseRecyclerViewAdapter.ViewHolder> {
    private static final String TAG = "CourseRecyclerViewAdapt";
    String[] strnum={"零","一","二","三","四","五","六"};
    String[] strtime={
            "","8:00-9:35","9:55-11:30","13:30-15:05",
            "15:20-16:55","17:10-18:45","19:30-21:05",
    };
    public ArrayList<CourseInfo> getCourseInfos() {
        return courseInfos;
    }

    public void setCourseInfos(ArrayList<CourseInfo> courseInfos) {
        this.courseInfos = courseInfos;
        notifyDataSetChanged();
    }

    private ArrayList<CourseInfo> courseInfos;
    private final Context context;

    public CourseRecyclerViewAdapter(Context context) {
        this.context=context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.layout_courses_adapter,parent,false);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                v.setEnabled(false);
                AnimatorSet set=new AnimatorSet();
                ObjectAnimator animator=ObjectAnimator.ofFloat(v,"translationX",0,-100);
                ObjectAnimator animator2=ObjectAnimator.ofFloat(v,"translationX",-100,100);
                ObjectAnimator animator3=ObjectAnimator.ofFloat(v,"translationX",100,0);
                animator.setDuration(100);
                animator2.setDuration(200);
                animator3.setDuration(100);
                set.playSequentially(animator,animator2,animator3);
                set.start();
                set.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        v.setEnabled(true);
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
            }
        });
        holder.tvName.setText(courseInfos.get(position).getName());
        String temp="第"+strnum[courseInfos.get(position).getTime()]+"节("+strtime[courseInfos.get(position).getTime()]+")";
        holder.tvTime.setText(temp);
        holder.tvWhen.setText(courseInfos.get(position).getWhen());
        holder.tvWhere.setText(courseInfos.get(position).getWhere());
        holder.tvWho.setText(courseInfos.get(position).getWho());
    }

    @Override
    public int getItemCount() {
        return courseInfos.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tvName;
        public TextView tvTime;
        public TextView tvWhen;
        public TextView tvWhere;
        public TextView tvWho;
        public ViewHolder(View itemView) {
            super(itemView);
            tvName=itemView.findViewById(R.id.tvName);
            tvTime=itemView.findViewById(R.id.tvTime);
            tvWhen=itemView.findViewById(R.id.tvWhen);
            tvWhere=itemView.findViewById(R.id.tvWhere);
            tvWho=itemView.findViewById(R.id.tvWho);
        }
    }
}
